﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SharpDiary.Core.Controls;
using SharpDiary.Core.ORMapping;
using SharpDiary.Core.Utility;

namespace SharpDiary.Task
{
    public partial class TaskInfoItemControl : ListItemControlBase
    {
        #region Fields & Properties

        private TaskInfo _task;

        public TaskInfo Task
        {
            get { return _task; }
            set
            {
                _task = value;
                DisplayItemInfo();
            }
        }

        private int _summaryMaxTextLength = 20;

        public int SummaryMaxTextLength
        {
            get { return _summaryMaxTextLength; }
            set { _summaryMaxTextLength = value; }
        }

        private Color _lblLeftDaysForeColor;

        #endregion

        #region Constructors

        public TaskInfoItemControl()
        {
            InitializeComponent();

            _lblLeftDaysForeColor = lblLeftDays.ForeColor;
        }

        #endregion

        #region Methods

        protected override void UpdateSelectedState(bool selected)
        {
            base.UpdateSelectedState(selected);

            if (selected)
            {
                lblLeftDays.ForeColor = Color.FromArgb(lblLeftDays.ForeColor.ToArgb() ^ 0xffffff);
            }
            else
            {
                lblLeftDays.ForeColor = _lblLeftDaysForeColor;
            }
        }

        protected override void BindControlsEvents(Control parentControl)
        {
            base.BindControlsEvents(parentControl);
        }

        protected override void DisplayItemInfo()
        {
            base.DisplayItemInfo();

            if (_task == null)
            {
                Title = string.Empty;
                Description = string.Empty;
                CreatedAndUpdateDatetime = string.Empty;
                progressBarDateRange.Value = 0;
                ImageAboveLogo = null;

                return;
            }

            Title = _task.Name;
            Description = Utils.GetSummaryText(_task.Description, _summaryMaxTextLength);
            CreatedAndUpdateDatetime = string.Format("{0} 至 {1}", _task.StartDatetime.ToLongDateString(), _task.EndDatetime.ToLongDateString());

            TaskStatusInfo status = TaskStatusInfo.GetTaskStatusInfoById(_task.Status);
            if (status != null && status.Picture.Length > 0)
            {
                if (status.Picture.Length > 0)
                {
                    ImageAboveLogo = Utils.BytesToImage(status.Picture);
                }
                else
                {
                    ImageAboveLogo = null;
                }

                if (!string.IsNullOrEmpty(status.Name))
                {
                    Title = _task.Name + " (" + status.Name + ")";
                }
            }

            if (_task.EndDatetime < DateTime.Now.Date)
            {
                lblLeftDays.Text = "已过期";
                progressBarDateRange.Value = progressBarDateRange.Maximum;
            }
            else if (_task.EndDatetime == DateTime.Now.Date)
            {
                lblLeftDays.Text = "今天到期";
                progressBarDateRange.Value = progressBarDateRange.Maximum;
            }
            else
            {

                int totalDays = (_task.EndDatetime - _task.StartDatetime).Days; //任务总天数
                int spentDays = (DateTime.Now.Date - _task.StartDatetime).Days; //已消耗的天数
                int leftDays = totalDays - spentDays;                           //剩余天数

                progressBarDateRange.Minimum = 0;
                progressBarDateRange.Maximum = totalDays;

                if (spentDays < 0)
                {
                    lblLeftDays.Text = "未开始";
                    progressBarDateRange.Value = 0;
                }
                else
                {
                    if (leftDays > progressBarDateRange.Maximum)
                    {
                        lblLeftDays.Text = "已过期";
                        progressBarDateRange.Value = progressBarDateRange.Maximum;
                    }
                    else
                    {
                        lblLeftDays.Text = string.Format("剩余({0}天):", leftDays);
                        progressBarDateRange.Value = spentDays;
                    }
                }
            }
        }

        protected override void OnEditButtonClick(object sender, EventArgs e)
        {
            base.OnEditButtonClick(sender, e);

            UserSettings.ShellInstance.ExecuteAction("SharpDiary.Task", "21aee42e-c54f-4201-864a-605390dd78fd", _task, this);
        }

        /// <summary>
        /// 删除当前任务。
        /// </summary>
        private void DeleteTask()
        {
            if (_task == null) { return; }

            if (MessageBoxHelper.ShowConfirmation("您确定要删除此任务吗？", true) == DialogResult.Yes)
            {
                if (TaskInfo.Remove<TaskInfo>(_task))
                {
                    this.Hide();
                    this.Dispose();
                }
                else
                {
                    MessageBoxHelper.ShowAlert("删除任务失败。");
                }
            }
        }

        protected override void OnDeleteButtonClick(object sender, EventArgs e)
        {
            base.OnDeleteButtonClick(sender, e);

            DeleteTask();
        }

        #endregion
    }
}
